/*
 * 导入外部js文件
 */
document.write("<script language=javascript src='/js/common.js'></script>");

var telephonePattern = /^(13[0-9]|14[5|7]|15[0-9]|18[0-9])\d{8}$/;
//var telephonePattern =/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
var passwordPattern = /^((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{8,16}$/;

var codePattern = /^\d{6}$/;

var telephonePassed = false, codePassed = false, passwordPassed = false, agreePassed=false;
var CodeGain = false;
function setAgreePassed(enable){
	if(enable){
		agreePassed=true;
		checkAllPassed();
	}else{
		agreePassed=false;
		checkAllPassed();
	}
}

/**
 * 检测手机号码是否被使用
 * @param telephone
 * @returns
 */
function checkTelephone(telephone){
	var msg = document.getElementById("telephoneMsg");
	var codeBtn = document.getElementById("verifyCodeBtn");
	if(!telephonePattern.test(telephone)){
		msg.innerHTML = "手机号码格式不正确";
		codeBtn.className="verify-btn-disabled";
		codeBtn.disabled=true;
		telephonePassed = false;
		checkAllPassed();
		return ;
	}
	msg.innerHTML = "";
	ajax({
		method:"POST",
		url:"/telephone/check",
		data:"telephone="+telephone,
		onsuccess:function(responseText){
			var result = eval("("+responseText+")");
			if(!result.success){
				msg.innerHTML="telephone used";
				telephonePassed = false;
				checkAllPassed();
			}else{
				telephonePassed = true;
				codeBtn.className="verify-btn";
				codeBtn.disabled=false;
				checkAllPassed();
			}
		}
	});
}

/**
 * 获取手机验证码
 * @returns
 */
function getVerifyCode(){
	
	var telephone = document.getElementById("telephone").value;
	//alert(telephone);
	ajax({
		url:"/telephone/code",
		method:"POST",
		data:"telephone="+telephone,
		onsuccess:function(responseText){
			var result = eval("("+responseText+")");
			if(result.success){
				var codeBtn = document.getElementById("verifyCodeBtn");
				codeBtn.className="verify-btn-disabled";
				codeBtn.disabled=true;
				codeGain = true;
				checkAllPassed();
				
				var expired = 300*1000;
				var count = 1;
				var intervalId = setInterval(function(){
					codeBtn.innerHTML="已发送("+(expired-1000*count++)/1000+")";
				},1000);
				var timeoutId = setTimeout(function(){
					codeBtn.innerHTML="获取验证码";
					codeBtn.className="verify-btn";
					codeBtn.disabled=false;
					codeGain = false;
					checkAllPassed();
					clearInterval(intervalId);
					clearTimeout(timeoutId);
				}, expired);
			}
		}
	});
}

/**
 * 检测验证码
 * @param code
 * @returns
 */
function checkCode(code){
	var codeMsg = document.getElementById("codeMsg");
	if(!codePattern.test(code)){
		codeMsg.innerHTML = "6位数字验证码";
		codePassed = false;
		checkAllPassed();
		return ;
	}
	codeMsg.innerHTML = "";
	codePassed = true;
	checkAllPassed();
}

/**
 * 密码校验
 * @param password
 * @returns
 */
function checkPassword(password){
	var msg = document.getElementById("passwordMsg");
	if(!passwordPattern.test(password)){
		msg.innerHTML="至少8位2种字符";
		passwordPassed = false;
		checkAllPassed();
		return ;
	}
	msg.innerHTML="";
	passwordPassed = true;
	checkAllPassed();
};

/**
 * 检测所有的数据格式是否已通过
 * @returns
 */
function checkAllPassed(){
	var submitBtn = document.getElementById("submit");
	if(!telephonePassed || !codePassed || !passwordPassed || !agreePassed || !codeGain){
		submitBtn.className="disabled";
		submitBtn.disabled = true;
	}else{
		submitBtn.className="btn";
		submitBtn.disabled = false;
	}
}

/**
 * 提交注册表单
 * @returns
 */
function sub(){
	var telephone = document.getElementById("telephone").value;
	var verifyCode = document.getElementById("verifyCode").value;
	var password = document.getElementById("password").value;
	ajax({
		url:"/register",
		method: "POST",
		data:"telephone="+telephone+"&verifyCode="+verifyCode+"&password="+password,
		onsuccess:function(responseText){
			console.log(responseText);
			var result = eval("("+responseText+")");
			if(result.success){
				document.location.href="/login";
			}else{
				var errors = result.params.errors;
				var msg = document.getElementById("codeMsg");
				msg.innerHTML = errors;
			}
		}
	});
}


